package BinaryTree;

import java.util.ArrayList;
import java.util.List;

public class preorderTraversal {

}
/**
 * Definition for a binary tree node.
 */
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        preTraversal(res,root);
        return res;
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        postTraversal(res, root);
        return res;
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        inTraversal(res, root);
        return res;
    }

    private void inTraversal(List<Integer> res, TreeNode root) {
        if (root == null) return;
        inTraversal(res, root.left);
        res.add(root.val);
        inTraversal(res, root.right);
    }

    void preTraversal(List<Integer> res, TreeNode root) {
        if(root==null){
            return;
        }
        res.add(root.val);
        preTraversal(res,root.left);
        preTraversal(res,root.right);
    }
    void  postTraversal(List<Integer> res, TreeNode root) {
        if(root==null){
            return;
        }
        postTraversal(res,root.left);
        postTraversal(res,root.right);
        res.add(root.val);
    }
}